﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace xtraSMSGui
{
    public partial class frmSend : Form
    {
        private xtraSMSCore.Core mainCore;
        private xtraSMSCore.Outlook outlook;
        private DataTable contacts;

        public frmSend(xtraSMSCore.Core Core)
        {
            InitializeComponent();
            mainCore = Core;
            if (mainCore.UseOutlook)
            {
                outlook = new xtraSMSCore.Outlook();
                contacts = outlook.getContacts();
                tbFilter.Enabled = true;
            }
            setInformation();
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            mainCore.ReLogin();
            if (mainCore.SendSMS(tbRecipient.Text, tbMessage.Text))
            {
                clearForm();
                writeStatusSuccess("Sending SMS, successfull.");
            }
            else
            {
                writeStatusFail("Sending SMS, failed!!!");
            }
            this.Enabled = true;
        }

        private void writeStatusSuccess(string message)
        {
            lblStatus.ForeColor = System.Drawing.Color.Green;
            lblStatus.Text = message;
        }

        private void writeStatusFail(string message)
        {
            lblStatus.ForeColor = System.Drawing.Color.Red;
            lblStatus.Text = message;
        }

        private void clearForm()
        {
            tbMessage.Text = string.Empty;
            tbRecipient.Text = string.Empty;
            tbFilter.Text = string.Empty;
            lblStatus.Text = string.Empty;
            setInformation();
        }

        private void setInformation()
        {
            xtraSMSProviderHandler.AccountInformation accInfo = mainCore.GetInformation();
            lblInformation.Text = accInfo.AccountName + " SMS Left: " + accInfo.LeftSMS;
        }

        private void timerFilter_Tick(object sender, EventArgs e)
        {
            timerFilter.Stop();
            if (tbFilter.Text != string.Empty)
            {
                string searchText = tbFilter.Text.ToLower();
                lvwContacts.Visible = true;
                lvwContacts.Clear();
                lvwContacts.Columns.Add("Nachname").Width = 100;
                lvwContacts.Columns.Add("Vorname").Width = 80;
                lvwContacts.Columns.Add("Natelnummer").Width = 120;
                foreach (DataRow row in contacts.Rows)
                {
                    if (row[0].ToString().ToLower().Contains(searchText) || row[1].ToString().ToLower().Contains(searchText) || row[2].ToString().ToLower().Contains(searchText))
                    {
                        ListViewItem lvmRow = new ListViewItem(new string[] { row[0].ToString(), row[1].ToString(), row[2].ToString() });
                        lvwContacts.Items.Add(lvmRow);
                    }
                }
            }
            else
            {
                lvwContacts.Visible = false;
            }            
        }

        private void addMultiContact()
        {
            foreach (ListViewItem multiContact in lvwContacts.SelectedItems)
            {
                tbRecipient.Text += multiContact.SubItems[2].Text + "; ";
                lvwContacts.Visible = false;
                tbFilter.Text = string.Empty;
            }
            tbMessage.Focus();
        }

        private void tbFilter_Click(object sender, EventArgs e)
        {
            tbFilter.Text = string.Empty;
        }

        private void lvwContacts_DoubleClick(object sender, EventArgs e)
        {
            addMultiContact();
        }

        private void lvwContacts_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                addMultiContact();
            }
        }

        private void tbMessage_TextChanged(object sender, EventArgs e)
        {
            lblLetters.Text = tbMessage.TextLength.ToString();
        }

        private void tbFilter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                if (lvwContacts.Items.Count > 0)
                {
                    lvwContacts.Focus();
                    lvwContacts.Items[0].Selected = true;
                }
            }
            else
            {
               timerFilter.Enabled = true;
            }
        }
    }
}
